package view;

import controller.FunctionController;
import controller.Controller;
import controller.impl.FunctionControllerImpl;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import model.container.ResourceDispatcher;

/**
 * Created by IntelliJ IDEA.
 * User: QIUKX of Freshpower.com.cn
 * Date: 2008-2-25
 * Time: 16:33:58
 */
public class ListPanel extends JPanel {
    private JFrame parent;
    private Controller controller;

    public ListPanel(JFrame parent,Controller controller) {
        this.parent = parent;
        this.controller = controller;
        try {
            this.controller.setDispatcher(new ResourceDispatcher());
        } catch (Exception e) {
            e.printStackTrace();
        }
        setSize(600, 500);
        draw();
    }

    private int draw() {
        JScrollPane scroll = new JScrollPane();
        scroll.setSize(600, 500);
        add(scroll, BorderLayout.CENTER);

        JTable table = new JTable(new MyTableModel());
        table.setPreferredScrollableViewportSize(new Dimension(500, 400));
        scroll.getViewport().add(table, null);
        return 0;
    }

    private class MyTableModel extends AbstractTableModel {
        private boolean DEBUG = true;
        private final String[] columnNames = {
                "A",
                "B",
                "C",
                "D",
                "E"
        };
        private final Object[][] data = {
                {"1", "2", "3", "4", "5"},
                {"6", "7", "8", "9", "10"},
                {"11", "12", "13", "14", "15"},
                {"1", "2", "3", "4", "5"},
                {"6", "7", "8", "9", "10"},
                {"1", "2", "3", "4", "5"},
                {"6", "7", "8", "9", "10"},
                {"1", "2", "3", "4", "5"},
                {"6", "7", "8", "9", "10"},
                {"1", "2", "3", "4", "5"},
                {"6", "7", "8", "9", "10"},
                {"1", "2", "3", "4", "5"},
                {"6", "7", "8", "9", "10"},
                {"1", "2", "3", "4", "5"},
                {"6", "7", "8", "9", "10"},
                {"1", "2", "3", "4", "5"},
                {"6", "7", "8", "9", "10"},
                {"6", "7", "8", "9", "10"},
                {"1", "2", "3", "4", "5"},
                {"6", "7", "8", "9", "10"},
                {"1", "2", "3", "4", "5"},
                {"6", "7", "8", "9", "10"},
                {"1", "2", "3", "4", "5"},
                {"6", "7", "8", "9", "10"},
                {"1", "2", "3", "4", "5"},
                {"6", "7", "8", "9", "10"},
                {"1", "2", "3", "4", "5"},
                {"6", "7", "8", "9", "10"},
                {"1", "2", "3", "4", "5"},
                {"6", "7", "8", "9", "10"},
                {"1", "2", "3", "4", "5"},
                {"6", "7", "8", "9", "10"},
                {"1", "2", "3", "4", "5"},
                {"6", "7", "8", "9", "10"}
        };

        public int getRowCount() {
            return data.length;
        }

        public int getColumnCount() {
            return columnNames.length;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            return data[rowIndex][columnIndex];
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        public boolean isCellEditable(int row, int col) {

//         if (col < 2) {
//           return false;
//         } else {
//           return true;
//         }
            return true;
        }

        public void setValueAt(Object value, int row, int col) {
            if (DEBUG) {
                System.out.println("Setting value at " + row + "," + col
                        + " to " + value
                        + " (an instance of "
                        + value.getClass() + ")");
            }
            if (data[0][col] instanceof Integer
                    && !(value instanceof Integer)) {
                try {
                    data[row][col] = new Integer(value.toString());
                    fireTableCellUpdated(row, col);
                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(parent,
                            "The " + getColumnName(col)
                                    + " column accepts only integer values.");
                }
            } else {
                data[row][col] = value;
                fireTableCellUpdated(row, col);
            }
            if (DEBUG) {
                System.out.println("New value of data:");
                printDebugData();
            }
        }

        private void printDebugData() {
            int numRows = getRowCount();
            int numCols = getColumnCount();
            for (int i = 0; i < numRows; i++) {
                System.out.print(" row " + i + ":");
                for (int j = 0; j < numCols; j++) {
                    System.out.print(" " + data[i][j]);
                }
                System.out.println();
            }
            System.out.println("--------------------------");
        }

    }
}
